// (c) Copyright 2013-2014 Stephen Gold <sgold@sonic.net>
// Distributed under the terms of the GNU General Public License

/*
 This file is part of the Bats Game.

 The Bats Game is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by the
 Free Software Foundation, either version 3 of the License, or (at your
 option) any later version.

 The Bats Game is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.

 You should have received a copy of the GNU General Public License
 along with the Bats Game.  If not, see <http://www.gnu.org/licenses/>.
 */
package bats.control;

import bats.SpatialProperties;
import bats.scene.SceneControl;
import bats.scene.SceneNode;
import java.util.logging.Level;
import java.util.logging.Logger;
import jme3utilities.MyString;

/**
 * A scene control which, when enabled, deletes its spatial from the scene.
 * <p>
 * Each instance is disabled at creation.
 *
 * @author Stephen Gold <sgold@sonic.net>
 */
public class DeleteControl
        extends SceneControl {
    // *************************************************************************
    // constants

    /**
     * message logger for this class
     */
    final private static Logger logger =
            Logger.getLogger(DeleteControl.class.getName());
    // *************************************************************************
    // constructors

    /**
     * Instantiate a disabled control in a specified scene.
     *
     * @param scene which scene contains the spatial (not null)
     */
    public DeleteControl(SceneNode scene) {
        super(scene);
        super.setEnabled(false);
    }
    // *************************************************************************
    // SimpleControl methods

    /**
     * Callback invoked when the spatial's geometric state is about to be
     * updated, once per frame while attached and enabled.
     *
     * @param updateInterval time interval between updates (in seconds, &ge;0)
     */
    @Override
    protected void controlUpdate(float updateInterval) {
        super.controlUpdate(updateInterval);
        if (spatial == null) {
            return;
        }
        delete();
    }
    // *************************************************************************
    // SceneControl methods

    /**
     * Remove the spatial from the scene, along with its children, controls,
     * water features, and lights.
     */
    @Override
    public void delete() {
        if (SpatialProperties.isHidden(spatial)) {
            logger.log(Level.WARNING,
                    "Timed out while attempting to add {0}, "
                    + "likely due to a physical conflict.",
                    MyString.quote(spatial.getName()));
            logger.log(Level.WARNING, "Try adding at a different location.");
        }
        logger.log(Level.INFO, "spatial={0}",
                MyString.quote(spatial.getName()));
        super.delete();
    }
}